<?php
require_once dirname(__FILE__) . '/../includes/common.inc.php';
require_once dirname(__FILE__) . '/../includes/pdoorm.inc.php';

header('Content-Type: text/plain');

$q = json_decode(MiscUtils::getParam('q', NULL));
/*print_r($q);
die;*/

  foreach ($q as &$tmp) {
    $tablename = $tmp->n;
    if ($tablename) {
    	if(is_array($tmp->a))
    	{
    	    foreach ($tmp->a as &$obj) 
    	    {
		        if (isset($obj->e_oid) || isset($obj->id)) 
		        {
		          pdoupdate($obj,$tablename);
		        } else {
		          pdoadd($obj,$tablename);
		        }
		    }    		
    		
    	}

      
	    if(isset($tmp->r))
	    {	
		      if(is_array($tmp->r))
		      {
		         foreach ($tmp->r as &$obj)
		         {
		            pdoremove($obj,$tablename);
		         }      	
		      }
	    }
    }
    
  }

  /****************************************************数据操作**************************************/
/**
 *
 * pdoupdate 
 */    
 function pdoupdate($obj,$tablename,$debug='')
 {
 	if(!empty($debug))
 	{
 		print_r($obj);
 	}
 	
 	if (!empty($obj))
 	{
 		
 		$producefield = "update ns_".$tablename." set ";
 		
 		foreach ($obj as $key => $value)
 		{
 			if(!in_array($key,array('pi_selects','v2_purchaseItems','pUsers','v2_events','v2_tracks','v2_produces',
 			                        'v2_notes','id','e_oid','_text','_method','_exist','_purchaseItems','_received')))
 			{
 				if(in_array($key,array('deadline','date','createdDate','sentDate','receivedDate','when')))
 				{
 					if(is_object($obj->$key))
 					{
 						$value = SimpleDate::toStamp($value);
 						if(empty($value)) $value = 0;
 					}else{
 						if(empty($obj->$key)) $value =0;
 					}
 					
 				}
 				
 			    $producefield .= '`'.$key."`='".addslashes($value)."',";
 			}
 			

           /*if(in_array($key,array('when','deadline')))
           {
               if(empty($obj->$key)) 
               {
               	  $obj->$key = 0;
                  $producefield .= " `$key` = '".$obj->$key."',";
               }else{
               	  $producefield .= " `$key` = '".SimpleDate::toStamp($obj->$key)."',";
               }
                    
               
           }*/        			
 			
 		}
 		
 		$producefield = substr($producefield,0,-1);
 		$producefield .=" where e_oid = '".$obj->e_oid."'";
 		
 		if(!empty($debug))
 		{
 			echo $producefield."\n";
 		}else{
 			sql_execute($producefield);
 		}
 		
 		/**
 		 * pi_selects operate
 		 * 
 		 */
// 		if(isset($obj->pi_selects))
// 		{
// 			if(!empty($obj->pi_selects))
// 			{
//               pi_selects_operate($obj->pi_selects,$debug);
 				
// 			}//not null
 			
// 		}//是否存在pi_selects
 		
 		/**
 		 * v2_purchaseItems operate
 		 */
 		if(isset($obj->v2_purchaseItems))
 		{
 			if(!empty($obj->v2_purchaseItems))
 			{
 				v2_purchaseItems_operate($obj->v2_purchaseItems,$debug);
 			}
 			
 		}//是否存在v2_purchaseItems  end v2_purchaseItems operate

 		if(isset($obj->v2_pUsers))
 		{
 			if(!empty($obj->v2_pUsers))
 			{
 				pusers_operate($obj->v2_pUsers,$debug);
 				
 			}//$obj->pUsers not null
 			
 		}//是否存在pUsers，  end pUsers operate
 		
 		/**
 		 * v2_events
 		 */
        if(isset($obj->v2_events))
        {
        	if(!empty($obj->v2_events))
        	{
        		v2_events_operate($obj->v2_events,$debug);
        	}
        	
        }//是否存在v2_events ，end v2_events operate

        if(isset($obj->v2_tracks))
        {
        	if(!empty($obj->v2_tracks))
        	{
        		v2_tracks_operate($obj->v2_tracks,$debug);
        	}
        	
        }//exist  v2_tracks ,end v2_tracks operate
        
        if(isset($obj->v2_notes))
        {
        	if(!empty($obj->v2_notes))
        	{
        		v2_notes_operate($obj->v2_notes,$debug);
        		
        	}//v2_notes not null
        	
        	
        }//exist notes note null
        
        if(isset($obj->v2_produces))
        {
        	if(!empty($obj->v2_produces))
        	{
        		foreach ($obj->v2_produces as $v2produce)
        		{
        			if(isset($v2produce->id) || isset($v2produce->e_oid))
        			{//update 
        				$v2producesql = 'update ns_produce set ';
        				
        				foreach($v2produce as $key => $value)
        				{
        				    if(!in_array($key,array('pi_selects','v2_purchaseItems','pUsers','v2_events','v2_tracks','v2_produces','v2_notes','id','e_oid','_text',
        				                            'when','deadline','_received')))
                            {
                               if(in_array($key,array('deadline','createdDate','sentDate','receivedDate','date')))
                               {
                               	if(is_object($v2produce->$key))
                               	{
                               		$value = SimpleDate::toStamp($value);
                               	}else {
                               		if(empty($v2produce->$key)) $value = 0;
                               	}
                               }
                               $v2producesql .= '`'.$key."`='".addslashes($value)."',";
                            }  

	                       if(in_array($key,array('when','deadline')))
	                       {
	                           if(empty($v2produce->$key))
	                           {
	                           	  $v2produce->$key = 0;
	                           	  $v2producesql .= " `$key` = '".$v2produce->$key."',";
	                           }else{
	                               $v2producesql .= " `$key` = '".SimpleDate::toStamp($v2produce->$key)."',";	
	                           }
	                                
	                           
	                       }                             
        					
        				}
        				
        				
		                /*if(in_array($key,array('when')))
		                {
		                    $v2producesql .='`when`,'; 
		                    
		                    if(empty($v2produce->when)) $v2produce->when = 0;
		                    
		                    $v2producesql .= " '".$v2produce->when."',";
		                } */

          				
        				
                        $v2producesql =substr($v2producesql,0,-1);
                        
                        $v2producesql .= " where e_oid = '".$v2produce->e_oid."'";
                        
                        if(!empty($debug))
                        {
                            echo $v2producesql."\n";
                        }else{
                            sql_execute($v2producesql);
                        }
                        unset($key);
                        unset($value);

                        /**
                         * pi_selects
                         */
//                        if(isset($v2produce->pi_selects))
//                        {
//	                        if(!empty($v2produce->pi_selects))
//	                        {
//	                        	pi_selects_operate($v2produce->pi_selects,$debug);
//	                        }
//                        }
                        
                        /**
                         * v2_purchaseItems
                         */
                        if(isset($v2produce->v2_purchaseItems))
                        {
                        	if(!empty($v2produce->v2_purchaseItems))
                        	{
                        		v2_purchaseItems_operate($v2produce->v2_purchaseItems,$debug);
                        	}
                        }
                        
        				
        			}else{//add
			            $v2producefield = '';
			            $v2producevalue = '';
			            
			            foreach($obj->v2_produces as $key => $value)
			            {
			                if(!in_array($key,array('pi_selects','v2_pUsers','v2_purchaseItems','v2_events','v2_tracks','v2_produces','v2_notes','_exist')))
			                {
			                    $v2producefield .='`'.$key.'`,';
			
			                    if(in_array($key,array('deadline','createdDate','sentDate','receivedDate','date','when')))
			                    {
			                    	if(is_object($obj->v2_produces->$key))
			                    	{
			                    		$value = SimpleDate::toStamp($value);
			                    		if(empty($value)) $value = 0;
			                    	}else{
			                    		if(empty($obj->v2_produces->$key)) $value = 0;
			                    	}
			                    }
			                        
			                    $v2producevalue .= " '".addslashes($value)."',";                      
			                
			                }
			
			                /*if(in_array($key,array('when','deadline')))
			                {
			                    $v2producefield .='`$key`,'; 
			                    
			                    if(empty($obj->v2_produces->$key))
			                    {
			                    	$obj->v2_produces->$key = 0;
			                    	$v2producevalue .= " '".$obj->v2_produces->$key."',";
			                    	
			                    }else{
			                        $v2producevalue .= " '".SimpleDate::toStamp($obj->v2_produces->$key)."',";	
			                    }
			                    
			                    
			                }*/               
			            
			            
			            }
			
			            $v2producefield = substr($v2producefield,0,-1);
			            $v2producevalue = substr($v2producevalue,0,-1);
			
			            $v2producesql = "insert into ns_produce(`e_oid`,".$v2producefield.") values(NULL,".$v2producevalue.")";  
			             
			            if(!empty($debug))
			            {
			                echo $v2producesql."\n";         
			            }else{
			                sql_execute($v2producesql);
			            } 
			            
			            unset($key);
			            unset($value);

			            /**
			             * pi_selects
			             */
        			                        /**
                         * pi_selects
                         */
//                        if(isset($v2produce->pi_selects))
//                        {
//                            if(!empty($v2produce->pi_selects))
//                            {
//                                pi_selects_operate($v2produce->pi_selects,$debug);
//                            }
//                        }
                        
                        /**
                         * v2_purchaseItems
                         */
                        if(isset($v2produce->v2_purchaseItems))
                        {
                            if(!empty($v2produce->v2_purchaseItems))
                            {
                                v2_purchaseItems_operate($v2produce->v2_purchaseItems,$debug);
                            }
                        }
        				
        			}//end add
        		}
        	}
        	
        	
        }//exist v2_produces ,end v2_produces operate
 		
 		
 	}//obj not null
 	
 }//end pdoupdate

 
 /**
  * 
  * pdoadd
  */
 function pdoadd($obj,$tablename,$debug='')
 {
 	if(!empty($debug))
 	{
 		echo"\n";
 		echo"*********************以下为增加操作!**tablename:".$tablename."***************************";
 		echo"\n";
 		print_r($obj);
 	}
 	
 	if(!empty($obj))
 	{
            $producefield = '';
            $producevalue = '';
            
            foreach($obj as $key=>$value)
            {
            	if($tablename == 'produceuser')
            	{
            	    if(in_array($key,array('trackId','account','password','name','email','type','produce_id')))
                    {
                        $producefield .='`'.$key.'`,';
    
                        if(in_array($key,array('deadline','createdDate','sentDate','receivedDate','date')))
                        {
                        	if(is_object($obj->$key))
                        	{
                        		$value = SimpleDate::toStamp($value);
                        		if(empty($value)) $value = 0;
                        	}else{
                        		if(empty($obj->$key)) $value = 0;
                        	}
                        }
                            
                        $producevalue .= " '".addslashes($value)."',";                      
                    
                    }         		
            		
            	}else{
	                if(!in_array($key,array('pi_selects','v2_pUsers','v2_purchaseItems','v2_events','v2_tracks','v2_produces','v2_notes','_exist')))
	                {
	                    $producefield .='`'.$key.'`,';
	
	                    if(in_array($key,array('deadline','createdDate','sentDate','receivedDate','date','when','isExpired')))
	                    {
	                    	if(is_object($obj->$key))
	                    	{
	                    		$value = SimpleDate::toStamp($value);
	                    		if(empty($value)) $value = 0;
	                    	}else{
	                    		if(empty($obj->$key)) $value = 0;
	                    	}
	                    	
	                    }
	                        
	                    $producevalue .= " '".addslashes($value)."',";                      
	                
	                }
            	}

                /*if(in_array($key,array('when','isExpired')))
                {
                    $producefield .='`'.$key.'`,'; 
                    
                    if(empty($obj->$key))
                    {
                    	$obj->$key = 0;
                    	$producevalue .= " '".$obj->$key."',";
                    	
                    }else{
                        if($key == 'when'){
                        	$producevalue .= " '".SimpleDate::toStamp($obj->$key)."',";
                        }else{
                        	$producevalue .= " '".$obj->$key."',";
                        }
                        
                    }
                    
                }*/               
            
            
            }

            $producefield = substr($producefield,0,-1);
            $producevalue = substr($producevalue,0,-1);

            $producesql = "insert into ns_".$tablename."(`e_oid`,".$producefield.") values(NULL,".$producevalue.")";  
             
            if(!empty($debug))
            {
                echo $producesql."\n";         
            }else{
                sql_execute($producesql);
            } 
            
            unset($key);
            unset($value);
 		
 	}//obj not null
 	
 	
 	
 }
 
 /**
  * pdoremove
  */
 
   function pdoremove($obj,$tablename,$debug='')
   {
   	  if(!empty($debug))
   	  {
   	  	echo "\n";
   	  	echo "***********************tablename:".$tablename."**************************";
   	  	echo "\n";
   	  	print_r($obj);
   	  }
   	  
   	  if(!empty($obj))
   	  {
   	  	switch($tablename)
   	  	{
   	  		case'producetrack':
   	  			{
   	  				$deltracksql = "delete from ns_".$tablename." where trackId = '".$obj->trackId."'";
   	  				
   	  				if(!empty($debug))
   	  				{
   	  					echo $deltracksql."\n";
   	  				}else{
   	  					sql_execute($deltracksql);
   	  				}
   	  				
   	  				$deltrackusersql = "delete from ns_producetrackuser where track_trackId = '".$obj->trackId."'";
   	  				
   	  				if(!empty($debug))
   	  				{
   	  					echo $deltrackusersql."\n";
   	  				}else{
   	  					sql_execute($deltrackusersql);
   	  				}
   	  				
   	  			}
   	  			break;
   	  		default:
   	  			{
   	  				$sqldel = "delete from ns_".$tablename." where e_oid ='".$obj->e_oid."'";
   	  				
   	  				if(!empty($debug))
   	  				{
   	  					echo $sqldel."\n";
   	  				}else{
   	  					sql_execute($sqldel);
   	  				}
   	  				
   	  				
   	  			}	
   	  			break;
   	  	}
   	  }
   	  
   	  
   	
   	
   	
   	
   }
 
 
 
   

?>